<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2251 – This `!` is not on a condition and skips errexit. Use `&& exit 1` instead, or make sure `$?` is checked.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2251 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2251">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2
id="this--is-not-on-a-condition-and-skips-errexit-use--exit-1-instead-or-make-sure--is-checked">This
<code>!</code> is not on a condition and skips errexit. Use
<code>&amp;&amp; exit 1</code> instead, or make sure <code>$?</code> is
checked.</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2251.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="bu">set</span> <span class="at">-e</span></span>
<span id="cb1-2"><a href="SC2251.html#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="ot">! </span><span class="fu">false</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2251.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="bu">set</span> <span class="at">-e</span></span>
<span id="cb2-2"><a href="SC2251.html#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="fu">false</span> <span class="kw">&amp;&amp;</span> <span class="bu">exit</span> 1</span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p>ShellCheck has found a command inverted with <code>!</code> that may
have no effect. In particular, it does not appear as a condition in an
<code>if</code> statement or <code>while</code> loop, or as the final
command in a script or function.</p>
<p>The most common reason for this is thinking that it'll trigger
<code>set -e</code> aka <code>errexit</code> if a command succeeds, as
in the example. This is not the case: <code>!</code> will inhibit
errexit both on success and failure of the inverted command.</p>
<p>Using <code>&amp;&amp; exit</code> will instead exit when failure
when the command succeeds.</p>
<h3 id="exceptions">Exceptions:</h3>
<p>ShellCheck will not detect cases where <code>$?</code> is implicitly
or explicitly used to check the value afterwards:</p>
<pre><code>set -e;
check_success() { [ $? -eq 0 ] || exit 1; }
! false; check_success
! true; check_success</code></pre>
<p>In this case, you can <a href="ignore.html">ignore</a> the warning.</p>
<h3 id="related-resources">Related resources:</h3>
<ul>
<li>StackOverflow: <a
href="https://stackoverflow.com/questions/39581150/why-do-i-need-parenthesis-in-bash-set-e-and-negated-return-code/39582012">Why
do I need parenthesis In bash <code>set -e</code> and negated return
code</a></li>
</ul>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


